package com.lzb.play.basic.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

import androidx.annotation.Keep;

import com.blankj.utilcode.util.Utils;

/**
 * Desc: 广播接收器工具类
 */
@Keep
public class BroadcastReceiverUtil {

    /**
     * 注册广播接收器
     *
     * @param context
     * @param receiver
     * @param filter
     * @param flags
     */
    public static Intent registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter, int flags) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return context.registerReceiver(receiver, filter, flags);
        } else {
            return context.registerReceiver(receiver, filter);
        }
    }

    /**
     * 注册广播接收器
     *
     * @param receiver
     * @param filter
     * @param flags
     */
    public static Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, int flags) {
        return registerReceiver(Utils.getApp(), receiver, filter, flags);
    }

    /**
     * 注册广播接收器,默认对外
     *
     * @param context
     * @param receiver
     * @param filter
     */
    public static Intent registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return context.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED);
        } else {
            return context.registerReceiver(receiver, filter);
        }
    }

    /**
     * 注册广播接收器,默认对外
     *
     * @param receiver
     * @param filter
     */
    public static Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        return registerReceiver(Utils.getApp(), receiver, filter);
    }

    /**
     * 注册不对外的广播接收器
     *
     * @param context
     * @param receiver
     * @param filter
     */
    public static Intent registerReceiverNotExported(Context context, BroadcastReceiver receiver, IntentFilter filter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
        } else {
            return context.registerReceiver(receiver, filter);
        }
    }

    /**
     * 注册不对外的广播接收器
     *
     * @param receiver
     * @param filter
     */
    public static Intent registerReceiverNotExported(BroadcastReceiver receiver, IntentFilter filter) {
        return registerReceiverNotExported(Utils.getApp(), receiver, filter);
    }

    /**
     * 注销广播接收器
     *
     * @param context
     * @param receiver
     */
    public static void unregisterReceiver(Context context, BroadcastReceiver receiver) {
        context.unregisterReceiver(receiver);
    }

    /**
     * 注销广播接收器
     *
     * @param receiver
     */
    public static void unregisterReceiver(BroadcastReceiver receiver) {
        unregisterReceiver(Utils.getApp(), receiver);
    }

}
